/*
 * ToggleTransitPathLinks.java
 * 
 * Copyright 2008 David D. Emory
 * 
 * This file is part of Five Points. See <http://www.fpdev.org> for
 * additional information regarding the project.
 * 
 * Five Points is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Five Points is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with Five Points.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.fpdev.apps.rtemaster.actions;

import java.util.Iterator;
import java.util.List;
import org.fpdev.core.basenet.BLink;
import org.fpdev.core.basenet.BNode;
import org.fpdev.core.basenet.Path;
import org.fpdev.core.transit.SubRoute;
import org.fpdev.core.transit.TransitPath;
import org.fpdev.apps.rtemaster.RouteMaster;

/**
 *
 * @author demory
 */
public class ToggleTransitPathLinks extends TransitPathModificationAction {

  private List<BLink> links_;
  private BNode oldStartNode_,  oldEndNode_;
  private boolean addStops_;

  public ToggleTransitPathLinks(SubRoute sub, List<BLink> links, boolean addStops) {
    super(sub);
    links_ = links;
    addStops_ = addStops;
    
    String str = "toggle links:";
    for(BLink link : links)  str += " "+link.getID();
    System.out.println(str);
  }

  protected boolean modifyTransitPath(RouteMaster av) {

    TransitPath tPath = sub_.getPath();

    av.getEngine().clearTransitAnnotations();
    tPath.setAddStop(addStops_);

    oldStartNode_ = tPath.startNode();
    oldEndNode_ = tPath.endNode();

    boolean pathModified = false;

    // link removal case:
    if (links_.size() == 1 && tPath.containsLink(links_.get(0)) &&
            (links_.get(0) == tPath.startLink() || links_.get(0) == tPath.endLink())) {
      int remResult = tPath.removeLink(links_.get(0));
      if (remResult == 0) {
        av.msg("link removed");
        pathModified = true;
      }
    } // add first or second link case:
    else if (links_.size() == 1 && tPath.linkCount() <= 1) {
      tPath.addLink(links_.get(0));
      pathModified = true;
    } else { // all other cases

      Path appPath = new Path(av.getEngine());
      System.out.println("links size=" + links_.size());
      Iterator<BLink> linkIter = links_.iterator();
      while (linkIter.hasNext()) {
        appPath.addToEnd(linkIter.next());
      }
      System.out.println("append size=" + appPath.linkCount());
      if (tPath.append(appPath)) {
        pathModified = true;
      } else {
        System.out.println("append failed");
      }
    }

    return pathModified;
  }

  public String getName() {
    return "Toggle Transit Path Links";
  }

  // These methods used by RouteOps when determining whether/how to pan map view
  public BNode getOldStartNode() {
    return oldStartNode_;
  }

  public BNode getOldEndNode() {
    return oldEndNode_;
  }
}
